<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>验证密保</title>
    <link rel="stylesheet" href="css/conmmon.css">
    <link rel="stylesheet" href="css/login.css">
    <link rel="stylesheet" href="css/check_secret.css">
    <!-- 引入 js -->
    <script src="js/jquery.min.js"></script>
    <script src="js/commom.js"></script>
</head>

<body>
    <!-- 导航栏 -->
    <div class="nav">
        <img src="img//博客系统logo.jpg" alt="">
        <span class="title">我的博客系统</span>
        <!-- 用来占据中间位置 -->
        <span class="spacer"></span>
        <a href="blog_list.html">主页</a>
        <a href="reg.html">注册</a>
        <a href="login.html">登陆</a>
        <!-- <a href="#">注销</a> -->
    </div>
    <!-- 版心 -->
    <div class="login-container">
        <!-- 中间的登陆框 -->
        <div class="login-dialog" id="secret">
            <h3>验证我的密保</h3>
            <div class="row">
                <span>密保问题: </span>
                <input type="text" id="question" value="" readonly="readonly">
            </div>
            <div class="row">
                <span>密保答案</span>
                <input type="text" id="answer" placeholder="输入12字以内的密保答案">
            </div>
            <div class="row">
                <span>新密码</span>
                <input type="password" id="password" placeholder="请设置您的新密码">
            </div>
            <div class="row"> 
                <span>确认密码</span>
                <input type="password" id="password2" placeholder="请确认您的密码">
            </div>
            <div class="row">
                <button id="submit" onclick="mysub()">提交</button>
            </div>
            <!-- <a id="forget" href="#">忘记密码</a> -->
        </div>
    </div>
    
    <script>
        // 初始化密保问题
        function initQuestion() {
            var uid = getUrlValue("uid");
            if (uid === "") {
                alert("非法用户!")
                location.href="chack_username.html";
                return;
            }
            jQuery.ajax({
                url: "/usqu/initques",
                type: "POST",
                data: {"uid": uid},
                success: function(result) {
                    if (result != null && result.code === 200 && result.data != null) {
                        $("#question").attr("value", result.data);
                    } else {
                        alert("访问失败!")
                        location.href = "login.html";
                    }
                }
            });
        }
        initQuestion();

        // 验证密保和设置密码
        function mysub() {
            var answer = jQuery("#answer");
            var uid = getUrlValue("uid");
            if (uid === "" || uid <= 0) {
                alert("非法访问!");
                location.href = "login.html";
                return;
            }
            if (answer.val() === "") {
                alert("请输入密保答案!");
                answer.focus();
                return;
            }
            var password = jQuery("#password");
            var password2 = jQuery("#password2");
            // 1. 非空校验.
            if (password.val() === "") {
                alert("请输入新密码!")
                password.focus(); // 将鼠标光标设置到密码控件
                return;
            }
            if (password2.val() === "") {
                alert("确认密码为空!")
                password2.focus(); // 将鼠标光标设置到确认密码控件
                return;
            }
            // 2. 判断修改密码时的两次密码是否相同.
            if (password.val() !== password2.val()) {
                alert("密码不一致!请再次确认!")
                password2.focus(); // 将鼠标光标设置到确认密码控件
                return;
            }
            // 2. 发送 ajax 请求
            jQuery.ajax({
                // 请求的地址
                url:"/usqu/check",
                // 请求类型
                type:"POST",
                // 请求的参数
                data:{"answer": answer.val(), "uid": uid, "password": password.val()},
                // 回调函数
                success:function(result) {
                    // 响应的结果
                    if (result != null && result.code === 200 && result.data === 1) {
                        // 密保验证成功, 并修改密码成功!
                        alert("密码修改成功!");
                        location.href = "myblog_list.html";
                    } else if (result != null && result.code === -4 && result.data == null) {
                        alert("新旧密码相同!修改失败!");
                    } else {
                        alert("密保答案错误!请重试");
                        answer.focus();
                    }
                }
            });
            
        }
    </script>
</body>

</html>